<template>
  <!-- 工程建设标准规范清单 -->
  <base-breadcrumb class="allPlan">
    <searchData :searchData="searchArr" @search="search"></searchData>
    <a-card>
      <!-- 表格 -->
      <list-table
        :page-keys="['pageNum', 'pageSize', 'total']"
        ref="table"
        :columns="columns"
        :request="loadData"
        :statisticsConfig="{ use: true, statistics, temp }"
      >
        <!-- 新增 -->
        <a-button v-btnPermission="'tm_017_add_01'" slot="prefix" icon="plus" type="primary" @click="handleManage(2)"
          >新增</a-button
        >
        <div slot="prefix" class="mt12">
          <span style="color: red">备注：项目规范清单，需每年4月更新并上报。</span>
        </div>

        <template slot="index" slot-scope="text,record,index">
          {{index+1}}
        </template>

        <template slot="linkUrl" slot-scope="text">
          <a :href="text" target="_blank">{{text}}</a>
        </template>

        <span slot="operation" slot-scope="text, record">
          <MoreButton>
          <!-- <MoreButton> -->
          <a v-btnPermission="'tm_017_loo_04'" @click="handleManage(0, record)">查看</a>
          <!-- 编辑 -->
          <template
            v-if="record.auditStatus == 'DRAFT' || record.auditStatus == 'REJECTED' || record.auditStatus === 'PAST'"
          >
            <a @click="handleManage(record.auditStatus === 'PAST' ? -1 : 1, record)"  v-btnPermission="'tm_017_edi_02'">编辑</a>
          </template>
            <a @click="setVisibleLink(record)" v-btn-permission="'tm_017_con_07'">配置参考规范链接</a>
          <template v-if="record.auditStatus == 'REJECTED' || record.auditStatus == 'DRAFT'">
              <!-- 删除 -->
              <a
                style="color: #1890ff;"
                v-if="record.auditStatus == 'DRAFT' || record.auditStatus == 'REJECTED'"
                v-btnPermission="'tm_017_del_03'"
                @click="handleRemoveParams({ id: record.id, type: 1 })"
              >
                删除
              </a>
              <!-- 发起审批 -->
              <a
                style="color: #1890ff;"
                v-if="record.auditStatus == 'DRAFT' || record.auditStatus == 'REJECTED'"
                v-btnPermission="'tm_017_del_03'"
                @click="launchProcessReq({ id: record.id })"
              >
                发起审批
              </a>
              <!-- 审批 -->
              <a v-if="record.auditStatus !== 'DRAFT'" v-btnPermission="'tm_017_loo_04'">
                <approval-button :id="record.id" :auditStatus="record.auditStatus" /> </a
            >
          </template>
          <template v-else>
            <!-- 删除 -->
            <a
              v-if="record.auditStatus == 'DRAFT' || record.auditStatus == 'REJECTED'"
              v-btnPermission="'tm_017_del_03'"
            >
              <a @click="handleRemoveParams({ id: record.id, type: 1 })">删除</a>
            </a>
            <!-- 审批 -->
            <span v-if="record.auditStatus !== 'DRAFT'" v-btnPermission="'tm_017_loo_04'">
              <approval-button :id="record.id" :status="record.auditStatus" />
            </span>
          </template>
            </MoreButton>
        </span>

        <span slot="auditStatus" slot-scope="text, record">
          <list-status :status="record.auditStatus" :isApproval="true" conversion="en" :text="text"></list-status>
        </span>
      </list-table>
    </a-card>
    <!-- </a-col>
    </a-row> -->
    <selectApprover :businessId="businessId"  :processConfig="setProcessConfig" :isCloseUpper="false"></selectApprover>
    <a-modal v-model:visible="visibleLink" title="配置参考链接" @ok="handleOk" okText="提交">
      <a-form-model :model="form" :label-col="{ span: 4 }" :wrapper-col="{ span: 14 }">
        <a-form-model-item label="配置参考链接" class="requiredFormItem">
          <a-input v-model="form.referenceLink" />
        </a-form-model-item>
      </a-form-model>
    </a-modal>
  </base-breadcrumb>
</template>

<script>
import BasePage from '@/views/basePage' // 基础配置
import {
  getProjectStandardList,
  delProjectStandard,
  getProjectStandardCount,
  pushProjectStandard
} from '@/api/technical/specificationList'
import { updateReferenceLink } from '@/api/technology'
import TreeProject from '@/components/businessComponents/TreeProject' // 树
import MoreButton from '@/components/MoreButton'
import PermissionTreeOrganization from '@/components/businessComponents/PermissionTreeOrganization'
import SelectProject from '@/components/Select/SelectProject' // 项目
import SelectSubCompany from '@/components/Select/SelectSubCompany' // 分公司s
import OrgTreeSelect from '@/components/OrgTreeSelect'
import { status } from '../model.js'
const columns = [
  {
    title: '序号',
    scopedSlots: { customRender: 'index' }
  },
  {
    title: '年份',
    dataIndex: 'year',
    ellipsis: true
  },
  {
    title: '所属分公司',
    dataIndex: 'branchCompanyName',
    ellipsis: true
  },
  {
    title: '所属项目',
    dataIndex: 'projectName',
    ellipsis: true
  },
  {
    title: '生效日期',
    dataIndex: 'usedTime',
    ellipsis: true
  },
  {
    title: '是否新增',
    dataIndex: 'isInsert',
    ellipsis: true,
    customRender() {
      return ['否', '是'][arguments[0]] || '无'
    }
  },
  {
    title: '参考规范链接',
    dataIndex: 'referenceLink',
    scopedSlots: { customRender: 'linkUrl' }
  },
  {
    title: '状态',
    dataIndex: 'auditStatus',
    scopedSlots: { customRender: 'auditStatus' }
  },
  {
    title: '操作',
    dataIndex: 'operation',
    fixed: 'right',
    width: '180px',
    scopedSlots: { customRender: 'operation' }
  }
]

import { STable } from '@/components'
export default {
  name: 'a' + Date.now(),
  extends: new BasePage(),
  components: {
    STable,
    TreeProject,
    MoreButton,
    PermissionTreeOrganization
  },
  data() {
    this.columns = columns

    return {
      visibleLink: false,
      queryParam: {},
      // 统计
      statistics: {
        buildIng: 0,
        noUpdateNum: 0,
        updateNum: 0
      },
      temp: () => {
        return (
          <span style="margin-right: 12px">
            在建项目数量:{this.statistics.buildIng}&nbsp;&nbsp; 更新规范数量:
            {this.statistics.updateNum}
            &nbsp;&nbsp; 未更新规范数量:{this.statistics.noUpdateNum}
          </span>
        )
      },
      removeApi: { remove: delProjectStandard }, // api
      loadData: async parameter => {
        const requestParameters = Object.assign({}, parameter, { param: this.queryParam })
        this.getProjectStandardCountReq(this.queryParam)
        return getProjectStandardList(requestParameters).then(res => {
          if (res.code === '0') {
            res.data.list.forEach(i => {
              i.auditStatus = status[i.auditStatus]
            })
            return {
              code: 200,
              data: res.data.list,
              total: res.data?.total,
              currentPage: res.data?.pageNum
            }
          }
        })
      },
      searchArr: [
        {
          name: '所属单位',
          valueKey: 'searchQuery',
          type: OrgTreeSelect,
        },
        // {
        //   name: '所属分公司',
        //   valueKey: 'branchCompanyId',
        //   type: SelectSubCompany
        // },
        // {
        //   name: '所属项目',
        //   keyValue: 'branchCompanyId',
        //   valueKey: 'projectId',
        //   type: SelectProject
        // },
        {
          name: '年份',
          type: 'year',
          value: 'year',
          yearsOpen: false,
          attrs: {
            valueFormat: 'YYYY'
          }
        },
        {
          name: '是否新增',
          value: 'isInsert',
          type: 'select',
          data: [
            {
              value: '0',
              label: '否'
            },
            {
              value: '1',
              label: '是'
            }
          ]
        }
      ],
      //获取流程配置值
      processConfig: null,
      //点击提交发送流程配置值
      setProcessConfig: null,
      businessId: null,
      form: {
        referenceLink: ''
      }
    }
  },
 async mounted(){
   //获取通过获取流程配置值
    this.processConfig = await this.$getConfigFn('technology_project_standard')   
  },
  methods: {
    handleOk() {
      if(!this.form.referenceLink) {
        this.$message.warning('请填写完所有必填项信息')
        return
      }
      if(this.form.referenceLink.indexOf('http') === -1 && this.form.referenceLink.indexOf('https') === -1) {
        this.$message.warning('请填写正确的链接')
        return
      }
      const data = {
        id: this.form.id,
        referenceLink: this.form.referenceLink
      }
      updateReferenceLink(data).then(res=>{
        this.$message.success('提交成功')
        this.visibleLink = false;
        this.search()
      })
    },
    setVisibleLink(row) {
      this.form = { ...this.form, ...row };
      this.visibleLink = true;
    },
    getProjectStandardCountReq(requestParameters) {
      getProjectStandardCount(requestParameters).then(res => {
        this.statistics = res.data
      })
    },
    // 发起审批
    launchProcessReq(param) {
      this.$confirm({
        title: '温馨提示',
        content: '确定发起审批？',
        onOk: () => {
          return pushProjectStandard(param).then((res) => {
            this.$message.success('发起成功')
              //提交时，返回id
              this.businessId = param.id;
              this.setProcessConfig = this.processConfig;
              this.$refs.table.loadData()
          })
        }
      })
    },

    //搜索
    search(value) {
      value && (this.queryParam = value)
      this.$refs.table.refresh(true)
    },
    // 操作栏
    handleManage(type, record) {
      let query = {
        editModel: type
      }
      if (record && record.id) {
        query.id = record.id
      }
      this.$router.push({
        name: 'updateProjectStandard',
        query
      })
    }
  }
}
</script>
